
using TMPro;
using UnityEngine;
using UnityEngine.UI;
// 硬件测试
public class YingjianSet : MonoBehaviour
{
    [SerializeField] private SetSelect setSelect;
    [SerializeField] private GameObject Tooptip;
    [SerializeField] private Button[] button;
    public bool set;
    private int index;
    // 1p
    private int up1p;
    private int down1p;
    private int left1p;
    private int right1p;
    private int coin1p;
    private int confirm1p;
    // 2p

    // 数值文本
    [SerializeField] private TextMeshProUGUI up1pText;
    [SerializeField] private TextMeshProUGUI down1pText;
    [SerializeField] private TextMeshProUGUI left1pText;
    [SerializeField] private TextMeshProUGUI right1pText;
    [SerializeField] private TextMeshProUGUI coin1pText;
    [SerializeField] private TextMeshProUGUI confirm1pText;
    [Space]
    [SerializeField] private TextMeshProUGUI up2pText;
    [SerializeField] private TextMeshProUGUI down2pText;
    [SerializeField] private TextMeshProUGUI left2pText;
    [SerializeField] private TextMeshProUGUI right2pText;
    [SerializeField] private TextMeshProUGUI coin2pText;
    [SerializeField] private TextMeshProUGUI confirm2pText;
    [Space]//文字文本
    [SerializeField] private TextMeshProUGUI titleText;
    [SerializeField] private TextMeshProUGUI testText;
    [SerializeField] private TextMeshProUGUI exitText;

    void Start()
    {

    }

    void Update()
    {
        if (!set)
            GetSerial();
        YingjianConfirm();
        SetText();
    }

    private void YingjianConfirm()
    {
        if (Tooptip.activeSelf) return;
        if (Input.GetKeyDown(KeyCode.J) || ComTrans.Instance.GetsecSerial() || ComTrans.Instance.GetsetSerial())
        {
            confirm1p++;
            if (index == 0) return;
            if (!set)
            {
                Tooptip.SetActive(true);
                set = true;
                if (setSelect.language == 0)
                {
                    Tooptip.GetComponent<ToolTip>().SetupToolTipText("是否退出硬件测试", "YingjianDontSave");
                }
                else
                {
                    Tooptip.GetComponent<ToolTip>().SetupToolTipText("Do you want to exit hardware testing", "YingjianDontSave");
                }

                return;
            }
            else
            {
                set = false;
            }
        }
    }


    private void GetSerial()
    {
        if (Tooptip.activeSelf) return;
        if (Input.GetKeyDown(KeyCode.W) || ComTrans.Instance.GetVertucalU() == 1)
        {
            up1p++;
        }
        if (Input.GetKeyDown(KeyCode.S) || ComTrans.Instance.GetVertucalD() == -1)
        {
            down1p++;
        }
        if (Input.GetKeyDown(KeyCode.Q) || ComTrans.Instance.GetHorizontalL() == -1)
        {
            left1p++;
            if (--index < 0) index = button.Length - 1;
        }
        if (Input.GetKeyDown(KeyCode.E) || ComTrans.Instance.GetHorizontalR() == 1)
        {
            right1p++;
            if (++index >= button.Length) index = 0;
        }
        if (Input.GetKeyDown(KeyCode.U) || ComTrans.Instance.GetCoinSerial())
        {
            coin1p++;
        }
        // if (Input.GetKeyDown(KeyCode.I) || ComTrans.Instance.GetsecSerial())
        // {

        // }
    }

    private void SetText()
    {
        if (setSelect.language == 0)
        {
            titleText.text = "硬件测试";
            testText.text = "测试";
            exitText.text = "退出";
            up1pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "1p 上";
            down1pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "1p 下";
            left1pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "1p 左";
            right1pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "1p 右";
            confirm1pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "1p 确认";
            coin1pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "1p 投币";
            up2pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "2p 上";
            down2pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "2p 下";
            left2pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "2p 左";
            right2pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "2p 右";
            confirm2pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "2p 确认";
            coin2pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "2p 投币";
        }
        else
        {
            titleText.text = "Hardware Testing";
            testText.text = "Test";
            exitText.text = "Exit";
            up1pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "1p UP";
            down1pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "1p DOWN";
            left1pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "1p LEFT";
            right1pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "1p RIGHT";
            confirm1pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "1p CONFIRM";
            coin1pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "1p COIN";
            up2pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "2p UP";
            down2pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "2p DOWN";
            left2pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "2p LEFT";
            right2pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "2p RIGHT";
            confirm2pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "2p CONFIRM";
            coin2pText.transform.parent.GetChild(0).GetComponent<TextMeshProUGUI>().text = "2p COIN";
        }
        up1pText.text = up1p.ToString();
        down1pText.text = down1p.ToString();
        left1pText.text = left1p.ToString();
        right1pText.text = right1p.ToString();
        confirm1pText.text = confirm1p.ToString();
        coin1pText.text = coin1p.ToString();
        button[index].Select();
    }

    public void YingjianDontSave()
    {
        set = false;
        setSelect.ChangeSetUI(0);
        Debug.Log("退出");
    }
}
